package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/last-stone-weight-ii/description/
 * @create 2025/9/1 21:52
 * @since JDK17
 */

public class Solution41 {
    int f(int[] nums, int t) {
        boolean[] dp = new boolean[t + 1];
        int n = nums.length;
        dp[0] = true;
        for (int i = 0; i < n; i++) {
            for (int j = t; j >= nums[i]; j--) {
                dp[j] = dp[j] || dp[j - nums[i]];
            }
        }
        for (int i = t; i >= 0; i--) {
            if (dp[i]) {
                return i;
            }
        }
        return 0;
    }
    public int lastStoneWeightII(int[] stones) {
        int n = stones.length, sum = Arrays.stream(stones).sum(), rest = f(stones, sum / 2);
        return sum - rest - rest;
    }
}
